package com.labs;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import static com.labs.CopyFiles.copyFilesWithThreads;


/**
 * Hello world!
 */
public class App extends JFrame
{
    public static void main( String[] args ) throws IOException, InterruptedException {

        // 使用事件分发线程启动 Swing 应用
        SwingUtilities.invokeLater(() -> {
            try {
                new App().setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public App(){
        initializeUI();
    }

    private void initializeUI() {
        setTitle("文件处理工具");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setLocationRelativeTo(null); // 居中显示
        setResizable(false);

        // 创建按钮
        JButton button = new JButton("开始");
        button.setPreferredSize(new Dimension(100, 50));

        // 设置布局
        setLayout(new BorderLayout());
        add(button, BorderLayout.CENTER);

        // 按钮点击事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    openDirectory();
                } catch (IOException | InterruptedException ex) {
                    JOptionPane.showMessageDialog(App.this,
                            "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    private void openDirectory() throws IOException, InterruptedException {
        // 目录选择器
        // 创建文件选择器
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fileChooser.setDialogTitle("选择目录");
        int result = fileChooser.showOpenDialog(this);
        // System.out.println(file);
        if(result == JFileChooser.APPROVE_OPTION){
            File selectedDirectory = fileChooser.getSelectedFile();
            System.out.println(selectedDirectory.getCanonicalFile().toString());
            if (selectedDirectory != null && selectedDirectory.exists()) {
                // 获取cpu核心数
                int processors = Runtime.getRuntime().availableProcessors();
                // 打印CPU核心数
                // System.out.println("CPU cores: " + processors);

                // 获取项目所在目录
                // File file = new File(".");
                // File file = new File("G:\\files");
                String filePath = selectedDirectory.getCanonicalFile().toString();
                System.out.printf("filePath %s",filePath);
                // 获取所有文件包括目录
                File[] listFiles = selectedDirectory.listFiles();
                if(listFiles != null && listFiles.length > 0){
                    new Thread(() -> {
                        try {
                            copyFilesWithThreads(filePath, listFiles, processors * 2);
                            // 完成后显示提示
                            SwingUtilities.invokeLater(() ->
                                    JOptionPane.showMessageDialog(App.this,
                                            "文件处理完成!", "完成", JOptionPane.INFORMATION_MESSAGE));
                        } catch (Exception e) {
                            SwingUtilities.invokeLater(() ->
                                    JOptionPane.showMessageDialog(App.this,
                                            "处理过程中出错: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE));
                        }
                    }).start();

                    JOptionPane.showMessageDialog(this,
                            "文件处理已开始，请稍候...", "提示", JOptionPane.INFORMATION_MESSAGE);
                }else{
                    JOptionPane.showMessageDialog(this,
                            "选择的目录为空!", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
    }
}
